package com.fauks.study.msweb.controller;

import com.fauks.study.msweb.pojo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class WebController {

    @RequestMapping("/template")
    public String template(Model model) {
        model.addAttribute("msg", "hello template");
        model.addAttribute("time", new Date());
        model.addAttribute("sex", "男");
        model.addAttribute("age", "10");
        return "thymeleaf";
    }

    @RequestMapping("/stuList")
    public String stuList(Model model) {
        saveStuListToModel(model);
        return "userList";
    }

    @RequestMapping("/jspStu")
    public String jspStu(Model model) {
        System.out.println("jsp");
        saveStuListToModel(model);
        return "stuList";
    }

    @RequestMapping("/users")
    public String users(Model model) {
        saveStuListToModel(model);
        return "users";
    }

    @RequestMapping("/error500")
    public String error500() {
        throw new RuntimeException("error500");
    }

    @RequestMapping(value = "/error400", method = RequestMethod.POST)
    public String error400() {
        return "users";
    }

    @RequestMapping(value = "/customerror")
    public String error() {
        int i = 10 / 0;
        return "users";
    }

    private void saveStuListToModel(Model model) {
        List<Student> students = new ArrayList<>();
        students.add(new Student("张三", 11));
        students.add(new Student("李四", 12));
        students.add(new Student("王五", 13));
        model.addAttribute("stuList", students);
    }
}
